var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "components/common/base-component", "react", "components/common/component", "common", "mobile/services/shoppingCartService", "mobile/services/memberService", "mobile/services/shoppingService", "maishu-ui-toolkit"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const base_component_1 = require("components/common/base-component");
    const React = require("react");
    const component_1 = require("components/common/component");
    const common_1 = require("common");
    const shoppingCartService_1 = require("mobile/services/shoppingCartService");
    const memberService_1 = require("mobile/services/memberService");
    const shoppingService_1 = require("mobile/services/shoppingService");
    const maishu_ui_toolkit_1 = require("maishu-ui-toolkit");
    let ShoppingCartBar = class ShoppingCartBar extends base_component_1.BaseComponent {
        constructor(props) {
            super(props);
            this.state = { items: [] };
            this.member = this.createService(memberService_1.MemberService);
            this.shop = this.createService(shoppingService_1.ShoppingService);
        }
        buy() {
            return __awaiter(this, void 0, void 0, function* () {
                let items = this.state.items.filter(o => o.Selected);
                if (items.length <= 0)
                    return;
                var productIds = items.map(o => o.ProductId);
                var quantities = items.map(o => o.Count);
                let userInfo = yield this.member.userInfo();
                let memberName = userInfo.Mobile || '';
                let order = yield this.shop.createOrder(memberName, productIds, quantities);
                this.app.redirect("shopping/orderProducts", { id: order.Id });
                return order;
            });
        }
        componentDidMount() {
            common_1.subscribe(this, shoppingCartService_1.ShoppingCartService.productsCount, (value) => {
                this.setState({ productsCount: value });
            });
            common_1.subscribe(this, shoppingCartService_1.ShoppingCartService.items, (value) => {
                console.assert(value != null);
                this.setState({ items: value });
            });
        }
        render() {
            let { productsCount, items } = this.state;
            let totalAmount = 0;
            let selectedCount = 0;
            items.forEach(o => {
                totalAmount = totalAmount + o.Amount * o.Count;
                selectedCount = selectedCount + o.Count;
            });
            return (React.createElement("div", { className: "settlement" },
                React.createElement("div", { className: "pull-left", onClick: () => this.app.forward("shopping/shoppingCart") },
                    React.createElement("i", { className: "icon-shopping-cart" }),
                    productsCount ? React.createElement("span", { className: "badge bg-primary" }, productsCount) : null),
                React.createElement("div", { className: "pull-right" },
                    React.createElement("label", null,
                        "\u603B\u8BA1\uFF1A",
                        React.createElement("span", { className: "price" },
                            "\uFFE5",
                            totalAmount.toFixed(2))),
                    React.createElement("button", { className: "btn btn-primary", disabled: items.length == 0, ref: e => e ? maishu_ui_toolkit_1.buttonOnClick(e, () => this.buy()) : null }, "\u7ED3\u7B97"))));
        }
    };
    ShoppingCartBar = __decorate([
        component_1.component()
    ], ShoppingCartBar);
    exports.ShoppingCartBar = ShoppingCartBar;
});
